package com.xhwl.logistics.controller.logistics;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.fastjson.JSON;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.logistics.query.VehicleAccessQuery;
import com.xhwl.logistics.service.logistics.VehicleAccessService;
import com.xhwl.logistics.util.PageResult;
import com.xhwl.logistics.vo.logistics.VehicleAccessVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lin.yang
 * @since 2023-12-15
 */
@Api(tags = "车辆进出访问记录（物流数据统计）")
@RestController
@Slf4j
@RequestMapping("vehicle-access")
public class VehicleAccessController {


    @Autowired
    private VehicleAccessService vehicleAccessService;

    @ApiOperation(value = "分页车辆进出访问记录")
    @PostMapping(value = "pageVehicleAccess")
    public ResultJson<PageResult<VehicleAccessVO>> pageVehicleAccess(@RequestBody VehicleAccessQuery query){
        log.info("pageVehicleAccess() called with parameters => 【query = {}】",query);
        PageResult<VehicleAccessVO> vehicleAccessVOPageResult = vehicleAccessService.pageVehicleAccess(query);
        return ResultJson.success(vehicleAccessVOPageResult);
    }

    @ApiOperation(value = "导出车辆进出记录")
    @GetMapping(value = "exportVehicleAccess")
    public void exportVehicleAccess(VehicleAccessQuery query, HttpServletResponse response, HttpServletRequest request) throws IOException {
        log.info("exportVehicleAccess() called with parameters => 【query = {}】",query);
        String name= Optional.ofNullable(request).map(x->x.getHeader("name")).orElse(null);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //设置文件名
        String fileName = URLEncoder.encode(ObjectUtil.isNull(name)? "车辆进出记录":name,"UTF-8");// URLEncoder.encode(exportHistory.getFileName(), "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        List<VehicleAccessVO> list = this.vehicleAccessService.queryVehicleAccess(query);
        try {
            EasyExcel.write(response.getOutputStream(),VehicleAccessVO.class)
                    .excelType(ExcelTypeEnum.XLSX)
                    .autoCloseStream(Boolean.FALSE).sheet("错误报告")
                    .doWrite(list);
        } catch (IOException e) {
            e.printStackTrace();
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            Map<String, String> map = new HashMap<>(8);
            map.put("status", "failure");
            map.put("message", "下载文件失败" + e.getMessage());
            response.getWriter().println(JSON.toJSONString(map));
        }
    }
}
